package com.enlorenz.common.geo.gts.baidu;

import it.sauronsoftware.base64.Base64;

import java.net.SocketException;
import java.net.SocketTimeoutException;

import com.enlorenz.common.geo.base.GeoPoint;
import com.enlorenz.common.geo.base.IPoint;
import com.enlorenz.common.geo.gts.exception.TransPointException;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;

public class BaiduGtsUtil {
	private static Gson gson = new Gson();
	/**
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String [] args) throws Exception {
		IPoint point = getLonAndLat("112.987389", "28.195468");
		ResultInfoBean addr = getAddrInfoByXY(point,"1");
		System.out.println(addr.getResult().getFormatted_address());
	}
	
	public static ResultInfoBean queryGts(String lon,String lat) throws Exception{
		IPoint point =null;
		try{
			point = getLonAndLat(lon,lat);
		}catch (TransPointException e) {
			System.err.println(e.getMessage());
			return null;
		}
		ResultInfoBean addr = getAddrInfoByXY(point,"1");
		return addr;
	}
	
	public static ResultInfoBean queryGts(Double lon,Double lat) throws Exception{
		return queryGts(lon.toString(),lat.toString());
	}
	
	/**
	 * 获取百度的经纬度
	 * @param x mapac的经度
	 * @param y mapac的纬度
	 * @return 
	 * @throws Exception 
	 */
	private static com.enlorenz.common.geo.base.IPoint getLonAndLat(String x,String y) throws Exception{
		BaiduConvert baiduConvert = googleToBaidu(x,y);
		if(null ==baiduConvert || !baiduConvert.getError().equals("0")){
			throw new TransPointException("转换百度坐标出错:["+x+","+y+"]");
		}
		
		IPoint point=new GeoPoint();
		point.setLon(trans64Base(baiduConvert.getX()));
		point.setLat(trans64Base(baiduConvert.getY()));
		return point;
	}
	
	private static Double trans64Base(String val){
		return Double.valueOf(Base64.decode(val));
	}
	
	//api.map.baidu.com
	public static String BAIDU_CONVERT_URL="http://123.125.115.154/ag/coord/convert?from=2&to=4&x=%s&y=%s";
	public static String BAIDU_GTS_URL="http://123.125.115.154/geocoder/v2/?ak=40761968e23BE62dc269133c73f33762&location=%s,%s" +
			"&output=json&pois=%s";
	public static UrlTransfer urlTransfer=new UrlTransfer();
	/**
	 * 将坐标转为baidu 坐标
	 * @param x
	 * @param y
	 * @return
	 * @throws Exception
	 */
	private static BaiduConvert googleToBaidu(String x,String y) throws Exception{
		String url = String.format(BAIDU_CONVERT_URL, x,y);
		
		String readLine=readLine(url,  "gbk");
		if(null == readLine){
			return null;
		}
		
		BaiduConvert baiduConvert=gson.fromJson(readLine, BaiduConvert.class);
		return baiduConvert;
	}
	
	private static String readLine(String url,String code)throws Exception{
		String readLine=null;
		String urlTrans=urlTransfer.trans(url);
		try{
			readLine=com.enlorenz.util.UrlUtil.readUrl(urlTrans,code);
		}catch(SocketTimeoutException e){
			System.err.println("SocketTimeoutException:"+urlTrans);
			return null;
		}catch(SocketException e){
			System.err.println("SocketException:"+e.getMessage()+":"+urlTrans);
			return null;
		}
		return readLine;
	}
	
	private static ResultInfoBean getAddrInfoByXY(com.enlorenz.common.geo.base.IPoint point,String pois) throws Exception {
		String url = String
				.format(BAIDU_GTS_URL, point.getLat(),point.getLon(),pois);
		
		String readLine=readLine(url,  "utf-8");
		if(null == readLine){
			return null;
		}
		
		ResultInfoBean addr =null;
		try{
			addr = gson.fromJson(readLine,ResultInfoBean.class);
		}catch (JsonSyntaxException e) {
			throw new RuntimeException("获取的描述Json错误:["+point.getLon()+","+point.getLat()+
					"]["+readLine+"]",e);
		}
		if(null == addr || !addr.getStatus().equals("0")){
			throw new RuntimeException("获取百度位置描述失败["+point.getLon()+","+point.getLat()+
					"]");
		}
		return addr;
	}
	
	
	/**
	 * 获取机构代号
	 * @param result
	 * @return
	 */
	private static String getAreaCode(String key) {
		return GtsCodeData.getInstance().codeMap.get(key);
	}
	
}
